﻿/*
 * @author Rui Zhong
 * 依赖 recoob.helper, jquery validate
 * 支持select2，百度编辑器
 */
; (function ($) {
    $.recoobAjaxPage = function () {
        //处理ajax链接
        var handleAjaxLink = function () {
            $(document).on("click", ".js-ajax-button", function () {
                var ajaxUrl = $(this).data("url");
                var ajaxMethod = $(this).data("method") ? $(this).data("method").toUpperCase() : "POST";

                //页面上有多个ajax button，共享同一个callback时候，用于分辨的id
                var dataid = $(this).data("id");

                //回掉函数
                var callbackName = $(this).data("callback");
                var callback = function (result) {
                    if (callbackName && window[callbackName] && window[callbackName] instanceof Function) {
                        window[callbackName](result, dataid);
                    }
                }

                //点击前验证
                var valid = $(this).data("valid");
                if (valid && window[valid] && window[valid] instanceof Function) {
                    var msg = window[valid](dataid);
                    if (msg) {
                        $.recoobHelper.showMessage(msg);
                        return false;
                    }
                }

                var sendData = {};
                //附加数据
                var getdata = $(this).data("getdata");
                if (getdata && window[getdata] && window[getdata] instanceof Function) {
                    sendData = window[getdata](dataid);
                }

                $.recoobHelper.send(ajaxMethod, ajaxUrl, sendData, callback, null, true);

                return false;
            })
        }

        //查询的处理
        var handleSearch = function () {
            //重置按钮
            $(document).on("click", ".js-search-form-reset-button", function () {
                var $searchForm = $("#js-search-form");
                $("input[type=text]", $searchForm).val("");
                $("select", $searchForm).each(function () {
                    var $ele = $(this);
                    $ele.val($ele.find("option:first").val());
                    if ($ele.hasClass("select2")) {
                        $ele.trigger("change");
                    }
                });
                return false;
            });
        };

        //表单处理
        var handleForm = function () {
            $(document).on("click", ".js-ajax-submit", function () {
                $(this).parents("form").submit();
                return false;
            });

            //初始化select2插件
            $(".select2").each(function () {
                $this = $(this);
                var placeholder = $this.attr("placeholder");
                $this.select2({
                    width: '100%',
                    placeholder: placeholder,
                    language: "zh-CN",
                    templateResult: function (state) {
                        var txt = state.text;
                        if (txt.indexOf("*") == 0) {
                            return $('<span>&nbsp;&nbsp;&nbsp;&nbsp;' + txt.substring(1, txt.length) + '</span>');
                        }
                        else {
                            return $('<span><b>' + txt + '</b></span>');
                        }
                    }
                });
            });

            handleFormValidationAdditionalMethods();

            $(document).on("submit", ".js-ajax-form", function () {
                var $self = $(this);

                //验证前自定义扩展
                if (window.beforeAjaxFormValidate instanceof Function
                    && false === window.beforeAjaxFormValidate()) {
                    return false;
                }

                //验证表单
                if (!$self.valid()) {
                    return false;
                }

                //uEditor表单提交
                if (window.UE !== undefined && window.UE.instants) {
                    for (instant in window.UE.instants) {
                        window.UE.instants[instant].sync();
                    }
                }

                //提交前自定义扩展
                if (window.beforeAjaxFormSubmit instanceof Function
                    && false === window.beforeAjaxFormSubmit()) {
                    return false;
                }

                var confirmAttach = {};
                if (window.getConfirmAttachData instanceof Function) {
                    confirmAttach = window.getConfirmAttachData();
                }

                //回掉函数
                var callbackName = $self.data("callback");
                var callback = (callbackName && window[callbackName] && window[callbackName] instanceof Function)
                                ? window[callbackName]
                                : null;

                //ajax提交表单
                $.recoobHelper.sendAjaxForm($self, confirmAttach, callback, true);

                return false;
            });
        };

        //验证函数
        var handleFormValidationAdditionalMethods = function () {
            $.validator.addMethod("integer", function (value, element) {
                return this.optional(element) || /^-?\d+$/.test(value);
            }, "请输入正确的整数");

            $.validator.addMethod("decimal", function (value, element) {
                return this.optional(element) || /^\d+(\.\d{0,3})?$/.test(value);
            }, "情输入正确的金额");
        };

        //处理链接外包围的div为可点击
        var pointHandler = function () {
            $(document).on('click', '.pointA', function () {
                $a = $(this).find("a:first");
                var url = $a.attr("href");
                if($a.attr('target') == '_blank'){
                    window.open(url);
                    return false;
                }
                window.location.href = url;
                return false;
            });
        };

        (function init() {
            handleSearch();
            handleForm();
            handleAjaxLink();
            pointHandler();
        })();
    };
})(jQuery);